import { computed, inject, ref, useSlots, Text } from 'vue';
import 'element-plus/es/components/form/index.mjs';
import 'element-plus/es/components/config-provider/index.mjs';
import 'element-plus/es/hooks/index.mjs';
import { buttonGroupContextKey } from './constants.mjs';
import { useDeprecated } from 'element-plus/es/hooks/use-deprecated/index.mjs';
import { useGlobalConfig } from 'element-plus/es/components/config-provider/src/hooks/use-global-config.mjs';
import { useFormItem } from 'element-plus/es/components/form/src/hooks/use-form-item.mjs';
import { useFormSize, useFormDisabled } from 'element-plus/es/components/form/src/hooks/use-form-common-props.mjs';
import { useModalSize } from "@usehook"
import { ElMessageBox } from 'element-plus';

const useButton = (props, emit) => {
    useDeprecated({
        from: "type.text",
        replacement: "link",
        version: "3.0.0",
        scope: "props",
        ref: "https://element-plus.org/en-US/component/button.html#button-attributes"
    }, computed(() => props.type === "text"));
    const buttonGroupContext = inject(buttonGroupContextKey, void 0);
    const globalConfig = useGlobalConfig("button");
    const { form } = useFormItem();
    const _size = useFormSize(computed(() => buttonGroupContext == null ? void 0 : buttonGroupContext.size));
    const _disabled = useFormDisabled();
    const _ref = ref();
    const slots = useSlots();
    const _type = computed(() => props.type || (buttonGroupContext == null ? void 0 : buttonGroupContext.type) || "");
    const autoInsertSpace = computed(() => {
        var _a, _b, _c;
        return (_c = (_b = props.autoInsertSpace) != null ? _b : (_a = globalConfig.value) == null ? void 0 : _a.autoInsertSpace) != null ? _c : false;
    });
    const _props = computed(() => {
        if (props.tag === "button") {
            return {
                ariaDisabled: _disabled.value || props.loading,
                disabled: _disabled.value || props.loading,
                autofocus: props.autofocus,
                type: props.nativeType
            };
        }
        return {};
    });
    const shouldAddSpace = computed(() => {
        var _a;
        const defaultSlot = (_a = slots.default) == null ? void 0 : _a.call(slots);
        if (autoInsertSpace.value && (defaultSlot == null ? void 0 : defaultSlot.length) === 1) {
            const slot = defaultSlot[0];
            if ((slot == null ? void 0 : slot.type) === Text) {
                const text = slot.children;
                return /^\p{Unified_Ideograph}{2}$/u.test(text.trim());
            }
        }
        return false;
    });
    const emitEvent = (eventName, eventFunc) => {
        try {
            emit(eventName, eventFunc);
        } catch (e) {
            console.error(e)
        }
    }
    const handleClick = (evt) => {
        if (props.nativeType === "reset") {
            form == null ? void 0 : form.resetFields();
        }
        _ref.value.blur()
        if (props.confirmTitle) {
            useModalSize()
            ElMessageBox.confirm(`${props.confirmTitle}`, '提示', {
                dangerouslyUseHTMLString: true,
                confirmButtonText: ' ',
                cancelButtonText: ' ',
                type: 'warning',
                customClass: "c-dialog forbid-select",
                closeOnClickModal: false,
                cancelButtonClass: "iconfont icon-close button-auto-resize",
                confirmButtonClass: 'iconfont icon-check button-auto-resize',
                roundButton: true,
                showClose: false,
                distinguishCancelAndClose: true
            }).then(() => {
                emitEvent("click", evt);
            }).catch(_ => {
                emit("callback", _);
            }).finally(() => {
                useModalSize(false)
            })
        } else {
            emitEvent("click", evt);
        }
    };
    return {
        _disabled,
        _size,
        _type,
        _ref,
        _props,
        shouldAddSpace,
        handleClick
    };
};

export { useButton };
